<?php

class Client_Table_Role extends Apk_Db_Table
{
    protected $_name = 'accounts_roles';
	protected $_primary = 'id';
	protected $_dependentTables = array('Client_Table_RoleParent', 'Client_Table_Rule');	


    public function GetDataRow($id)
    {
    	$sel = $this->getAdapter()->select()
    				->from($this->_name)
    				->where('id = ?', $id);

    	return $this->setDecorator('simpleDecorator')->getList($sel)->getRow(0)->toArray();		
    }

    public function GetDataList($page = 1, $sort=null, $page_count=null)
    {
		$sel = $this->getAdapter()->select()
			->from($this->_name, array('id', 'name_full', 'desc'))
			->where('is_ghost = 0')
			->where('is_removed = 0')
			->order('norder');
			
		if(strlen($sort) > 0) $sel->order($this->_name.'.'.$sort);
		
		return $this->setDecorator('simpleDecorator')->getPager($sel, $page, array('perPage' => $page_count));
    }
    
    public function GetRoles($id = null)
    {		
    	$sel = $this->getAdapter()->select()
			->from($this->_name)
			->where('is_ghost = 0')
			->where('is_removed = 0')	
			->where('is_active')					
			->order('norder');					
	
		return $this->cache('GetRoles'.$id, 1200)->getList($sel)->toArray();
    }

    public function GetRoleByID($id)
    {		
    	$sel = $this->getAdapter()->select()
			->from($this->_name)
			->where('is_ghost = 0')
			->where('is_removed = 0')	
			->where('is_active')
			->where('id = ?', $id)					
			->order('norder');						
	
		$data = $this->getList($sel)->toArray();
		
		if(count($data) > 0) return $data[0];
		else return array();		
    }
    
    
    
    public function GetRolesList($a)
    {
		$sel = $this->getAdapter()->select()
			->from($this->_name, array('id', 'name', 'name_full'))
			->where('is_ghost = 0')
			->where('is_removed = 0')			
			->order('norder');
			
		if(strlen($a) > 0 && is_string($a))	$sel->where('id not in ('.$a.')');
		
		return $this->getList($sel)->toArray();
    }      
 

    public function GetParentsList($a = null, $id)
    {
    	$ret = array();

		$sel = $this->getAdapter()->select()
					->from('accounts_roles')
					->where('id != ?', $id);
		$data = $this->getList($sel)->toArray();

    	if(count($data) > 0 && !is_null($a))
    	foreach($data as $k => $v)
    	  	if($v['id'] == $a) $data[$k]['__attributes']['active'] = 1;

    	return $data;
    }

	public function insert(array $data, $user_id)
    {    	
    	$data['modify_date'] = time();
    	$data['add_date'] = time();
    	$data['add_user_id'] = $user_id;
    	$data['modify_user_id'] = $user_id;
        return parent::insert($data);
    }

    public function update(array $data, $id, $user_id)
    {
    	$data['modify_date'] = time();
    	$data['modify_user_id'] = $user_id;
    	$data['is_ghost'] = 0;   	    	
    	
		return parent::update($data, 'id = '.$id);
    }

    public function remove($items, $user_id)
    {    	
    	foreach($items as $k => $id)
    	{
	    	$data['modify_user_id'] = $user_id; 
	    	$data['modify_date'] = time();
	    	$data['is_removed'] = 1;
	    	
	    	parent::update($data, 'id = '.$id);    		
    	}
    	
    	return true;    	
	}

    /********************
     ***  Decorators  ***
    *********************/

    public function simpleDecorator($key, $value)
    {
        switch ($key) {            
            case 'modify_date':
            case 'add_date':
            	$result[$key]['__attributes'] = $this->timestampToArray($value);
            	break;
            default:
                $result[$key] = stripslashes($value);
                break;
        }
        return $result;
    }	
}
?>